function fix_jqgrid_header_and_body_width_bug(jqgrid_selector_str) {
    var colModels = $("#" + jqgrid_selector_str).jqGrid('getGridParam','colModel');
    for(var i = 0; i < colModels.length; ++i) {
        var col_key_str = colModels[i].name; //summary ...
        
        var colProp = $("#" + jqgrid_selector_str).getColProp(col_key_str);
        
        var col_th_obj = $("#" + jqgrid_selector_str + "_" + col_key_str); //id_new_voch_grid_summary
        var old_width = colProp.width;
        if(!colProp.hidden) {
            col_th_obj.css("width",  old_width + 1);    
        }
    }
}

var getColumnSrcIndexByName = function(grid_id, columnName) {
    var cm = $("#" + grid_id).jqGrid('getGridParam', 'colModel');
    var i = 0;
    var index = 0;
    while (i < cm.length) {
        var cmName = cm[i].name;
        i++;
        if (cmName === columnName) {
            return index;
        } else if (cmName !== 'rn' && cmName !== 'cb' && cmName !== 'subgrid') {
            index++;
        }
    }
    return -1;
}

function showOrHideColumn(grid_id, opt, columnNames) { //columnNames join with comma, example "col1, col2"
    var colArr = columnNames.split(",");
    if(0 == colArr.length) {
        return;
    }
    for(var i = 0; i < colArr.length; ++i) {
        colArr[i] = colArr[i].trim();
    }

    var oldWid = $("#" + grid_id).jqGrid('getGridParam', 'width');
    if("show" == opt) {
        $("#" + grid_id).jqGrid('showCol', colArr);
    } else {
        $("#" + grid_id).jqGrid('hideCol', colArr);
    }
    var newWid = $("#" + grid_id).jqGrid('getGridParam', 'width');
    var parentWid = $(".pz_new_body").width();
    //console.log("grid_id:" + grid_id + ", oldWid:" + oldWid + "newWid:" + newWid + ", parentWid:" + parentWid);

    var colModels = $("#" + grid_id).jqGrid('getGridParam','colModel');
    if(newWid < (parentWid - 2)) { //need shrinkToFit option is true to effecting
        for(var i = 0; i < colModels.length; ++i) {
            var col_key_str = colModels[i].name; //summary ...
            $("#" + grid_id).setColProp(col_key_str, {fixed:false});
        }
        $("#" + grid_id).jqGrid('setGridWidth', parentWid - 2, true);
    } else {
        for(var i = 0; i < colModels.length; ++i) {
            var col_key_str = colModels[i].name; //summary ...
        //    $("#" + grid_id).setColProp(col_key_str, {fixed:true});
        }
        $("#" + grid_id).jqGrid('setGridWidth', parentWid - 2, false);
    }
    
    fix_jqgrid_header_and_body_width_bug(grid_id);
}

function auto_update_grid(grid_id, ps_data) { //遵从约定 server给的key和grid的model.name一致
    var grid_items_count = ps_data.grid_data.length;
    var gridArrayData = [];
    for (var i = 0; i < grid_items_count; i++) {
        var item = ps_data.grid_data[i];
        var o = {};
        for(var j in item) {
            o[j] = item[j];
        }
        gridArrayData.push(o);
    }
    $("#" + grid_id).jqGrid('clearGridData').jqGrid('setGridParam', { data: gridArrayData}).trigger('reloadGrid');
}